package com.kfm.redisspringbootautoconfigure.config;


import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;

@Configuration
@EnableConfigurationProperties(RedisPropertie.class) // 开启属性注入
@ConditionalOnClass(Jedis.class) // 当Jedis这个类在类路径中时，才会创建一个Jedis的Bean
public class RedisAutoConfigure {


    @Bean
    @ConditionalOnProperty(name = "kfm.redis.host") // 当配置文件中 kfm.redis.host 属性存在时，才会创建一个Jedis的Bean
    public Jedis getJedis(RedisPropertie redisPropertie){
        if (redisPropertie == null) {
            throw new RuntimeException("redisPropertie is null");
        }
        int port = 6379;
        if (redisPropertie.getHost() != null) {
            port = redisPropertie.getPort();
        }
        return new Jedis(redisPropertie.getHost(), redisPropertie.getPort());
    }
}
